/**
 * 
 */
(function() {
    'use strict';

    CCR.Models.Customes = Backbone.Model.extend({
    	file_url:"customes/customes.txt",
    	// 添加收听频道
    	addChannelId:function(channel_id){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			//add id
				if(!isArray(data)){
					data = new Array();
				}
				//判断是否已存在
				var exist = false;
				for(var i = 0; i < data.length; i++){
					if(data[i].channel_id===channel_id){
						exist = true;
					}
				}
				if(exist){
					self.updateChannelId(channel_id);
				}else{
					self.saveChannelId(channel_id);
				}
    		});
    	},
    	// 修改收听次数
    	updateChannelId:function(channel_id){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			var updated_data = new Array();
				for(var i = 0; i < data.length; i++){
					if(data[i].channel_id===channel_id){
						updated_data.push({channel_id:data[i].channel_id,time:(data[i].time+1)});
						log("修改频道"+data[i].channel_id+"收听次数:"+(data[i].time+1));
						
					}else{
						updated_data.push({channel_id:data[i].channel_id,time:data[i].time});
					}
				}
				// 按照收听次数降序排列
				updated_data.sort(function(a, b) {
					//a在前，返回负数
			        //b在前,返回正数
			        //0，不变
					if(a.time>=b.time){
						return -1;
					}else{
						return 1;
					}
				 });
				sdk.io.writeJSONFile(self.file_url, updated_data, false);
    		});
    	},
    	// 添加保存首次收听的频道
    	saveChannelId:function(channel_id){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
				if(!isArray(data)){
					data = new Array();
				}
				data.push({channel_id:channel_id,time:1});
				log("添加收听频道"+channel_id+"收听次数:"+1);
				sdk.io.writeJSONFile(self.file_url, data, false);
    		});
    	},
    	// 获取已添加的频道
    	getChannelList:function(callback){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			//add id
				if(!isArray(data)){
					data = new Array();
				}
    			typeof callback ==="function"&&callback(data);
    		});
    	},
    	// 获取排列的频道
    	getArrangeChannelList:function(channel_list,callback){
    		var self = this;
    		self.getChannelList(function(data){
    			var list = new Array();
    			
    			//根据添加的频道排列
    			for(var i=0;i<data.length;i++){
    				var channel = getChannel(data[i].channel_id,channel_list);
    				list.push(channel);
    			}
    			//添加为记录的频道
    			for(var i=0;i<channel_list.length;i++){
    				var exist = false;
    				for(var j=0;j<data.length;j++){
    					if(channel_list[i].channelid===data[j].channel_id){
    						exist = true;
    					}
    				}
    				if(!exist){
    					list.push(channel_list[i]);
    				}
    			}
    			
    			//getChannel by id
    			function getChannel(channel_id,list){
    				for(var i=0; i<list.length;i++){
    					if(list[i].channelid==channel_id){
    						return list[i];
    					}
    				}
    			}
    			
    			var last = new Array();
    			//重新按照基偶关系前后排列
    			for(var i=0;i<list.length;i++){
    				if(i%2===0){
    					last.push(list[i]);
    				}else{
    					last.unshift(list[i]);
    				}
    			}
    			typeof callback==="function"&&callback(last);
    		});
    	},
    	
    });
})();